﻿using PosClient.Event;
using PosClient.Model;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PosClient.ViewModel
{
    public class SearchVipViewModel: ReactiveObject
    {
        private IScheduler _mainScheduler;

        private string _InputCode;
        public string InputCode
        {
            get => _InputCode;
            set => this.RaiseAndSetIfChanged(ref _InputCode, value);
        }

        private VipBase _Vip = new VipBase();
        public VipBase Vip
        {
            get => _Vip;
            set => this.RaiseAndSetIfChanged(ref _Vip, value);
        }

        public ReactiveCommand<Unit, Result<VipBase>> SearchVipCommand { get; set; }
        public SearchVipViewModel(IScheduler mainScheduler)
        {
            mainScheduler = _mainScheduler;

            var canSearch = this.WhenAnyValue(vm => vm.InputCode, (x) => !string.IsNullOrEmpty(x));

            SearchVipCommand = ReactiveCommand.Create(SearchVipCommandImpl, canSearch, mainScheduler);
        }

        private Result<VipBase> SearchVipCommandImpl()
        {
            Result<VipBase> result = new Result<VipBase>
            {
                Success = true,
                Message = "请求成功"
            };
            Thread.Sleep(2000);
            var vip = new VipBase()
            {
                VipCode = "800096423",
                VipName = "许先生",
                VipLevel = new VipLevel() 
                {
                    LevelCode = "3",
                    LevelName = "黄金会员",
                    LevelType = 1
                },
                Mobile = "13041064141",
                PointCurrent = 36,
                DepositCurrent = 500,
                GrowthCurrent = 50000,
                TicketCount = 5,
                Tickets = new List<Ticket>()
                {
                    new Ticket{ TicketNo = "5965648415445644", TicketName = "8折全品类券", TicketType = "优惠折扣券"  },
                    new Ticket{ TicketNo = "5965648415445645", TicketName = "10元券", TicketType = "代金券"  },
                    new Ticket{ TicketNo = "5965648415445646", TicketName = "德运奶10元券", TicketType = "单品代金券"  },
                    new Ticket{ TicketNo = "5965648415445647", TicketName = "会员生日礼券", TicketType = "礼品券"  },
                    new Ticket{ TicketNo = "5965648415445648", TicketName = "购纸巾减至10元券", TicketType = "减至券"  },
                },
                Lables = new List<string>()
                { 
                    "老客户","有效会员","30日内消费","累计消费过万"
                }
            };
            Vip = vip;
            MessageBus.Current.SendMessage(new SearchVipEventArgs() { Vip = vip });
            result.Data = Vip;
            return result;
        }
    }
}
